﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;

namespace BiffenGUI.UserControls
{
    public partial class MemberShowBookings : UserControl
    {
        private frmMember parent = null;
        private BiffenServiceClient service = null;
        private List<Booking> found = null;
        private int selectedResult = -1;
        private SearchElement noResults = new SearchElement(null, "Ingen booking blev fundet!");

        public MemberShowBookings()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="svc"></param>
        public void SetServiceReferance(frmMember frm, BiffenServiceClient svc)
        {
            parent = frm;
            service = svc;
            Initialize();
        }

        private void Initialize()
        {
            memberBookingsList.DisplayMember = "Value";
            memberBookingsList.ValueMember = "BookingId";
            delBookningButton.Enabled = false;
        }
        /// <summary>
        /// Method that clears the labels
        /// </summary>
        private void ClearInfoFields()
        {
            label3.Text = "";
            lblTime.Text = "";
            lblTheater.Text = "";
            lblSeats.Text = "";
            lblStatus.Text = "";
        }

        /// <summary>
        /// Method that creates a string version for a datetime object
        /// </summary>
        /// <param name="start">DateTime start the datetime wanted to make to a string</param>
        /// <returns>returns a string version og det datetime object </returns>
        private string GetShowTime(DateTime start)
        {
            return start.ToShortDateString() + " " + start.ToShortTimeString();
        }

        /// <summary>
        /// Method that fills the list with current bookings for the member logged in
        /// </summary>
        /// <param name="phone">int Phone the phonenumber of the member logged in</param>
        public void FillListWithBookings(int phone)
        {
            ClearInfoFields();
            found = service.FindBookingByPhone(phone);
 
            memberBookingsList.Items.Clear();
            if (found != null && found.Count > 0)
            {
                foreach (Booking b in found)
                {
                    memberBookingsList.Items.Add(new SearchElement(b, b.Show.Movies[0].Title + ": " + b.Show.StartTime.ToString()));
                }
            }
            else
            {
                memberBookingsList.Items.Add(noResults);
            }
        }
        
        /// <summary>
        /// Method that gets the current selected item in the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void memberBookingsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearInfoFields();
            if (memberBookingsList.SelectedIndex > -1)
            {
                selectedResult = memberBookingsList.SelectedIndex;
                SearchElement se = (SearchElement)memberBookingsList.Items[selectedResult];
                if (se.Booking != null)
                {
                    Booking b = se.Booking;

                    // Enable button
                    delBookningButton.Enabled = true;

                    // Update info:
                    lblMovie.Text = b.Show.Movies[0].Title;
                    lblTime.Text = b.Show.StartTime.ToString();
                    lblTheater.Text = b.Show.Theater.Id.ToString();
                    string seats = "";
                    foreach (int i in b.Seats)
                    {
                        if (seats != "")
                            seats += ", ";
                        seats += i.ToString();
                    }
                    lblSeats.Text = seats;

                    lblPriceValue.Text = (b.Show.Movies[0].PriceCategory.Price * b.Seats.Count).ToString();

                    string status = "Reserveret";
                    if (b.Status == Booking.States.Sold)
                    {
                        status = "Købt";
                    }
                    else if (b.Status == Booking.States.Released)
                        status = "Frigivet til andre";
                    lblStatus.Text = status;
                }
            }
            else
            delBookningButton.Enabled = false;
                
        }

        /// <summary>
        /// Method that deletes the current selected booking from the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delBookningButton_Click(object sender, EventArgs e)
        {
            Booking b = ((SearchElement)memberBookingsList.Items[selectedResult]).Booking;
            if (MessageBox.Show("Vil du slette denne booking?", "Slet?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (service.DeleteBookingById(b.ID) > 0)
                {
                    found.Remove(b);
                    memberBookingsList.Items.RemoveAt(selectedResult);
                    ClearInfoFields();
                    memberBookingsList.SelectedIndex = -1;
                    if (memberBookingsList.Items.Count < 1)
                    {
                        memberBookingsList.Items.Add(noResults);
                    }
                }
                else
                {
                    MessageBox.Show("Du skal vælge en bookning for at kunne slette");
                }
            }
        }

        public class SearchElement
        {
            public Booking Booking { get; set; }
            public string Value { get; set; }
            public SearchElement(Booking b, string val)
            {
                Booking = b;
                Value = val;
            }
        }
    }
}
